package me.ree.zkproduct.config;

import lombok.extern.slf4j.Slf4j;
import me.ree.zkproduct.service.IOrdersService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * 1.验证执行顺序，验证结果：
 * @Autowired > @PostConstruct > afterPropertiesSet > init-method
 *
 * 2.实现InitializingBean接口的类也必须是一个spring的bean，afterPropertiesSet才会被调用；
 * @PostConstruct和init-method也是如此
 */
@Slf4j
public class TestBean implements InitializingBean {

    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private TestEmptyConfig testEmptyConfig;


    @PostConstruct
    public void postConstructMethod() {
        // 执行顺序 3: @PostConstruct 注解的方法
        log.info("TestBean postConstructMethod exec......");

        log.info("TestBean postConstructMethod ordersService==null: {}", ordersService==null);

        log.info("TestBean postConstructMethod testEmptyConfig.name==null", testEmptyConfig==null);
        testEmptyConfig.setName("TestBean postConstructMethod name");
        log.info("TestBean postConstructMethod testEmptyConfig.name={}", testEmptyConfig.getName());
    }

    @Override
    public void afterPropertiesSet() {
        // 执行顺序 2: InitializingBean 接口的 afterPropertiesSet() 方法
        log.info("TestBean afterPropertiesSet exec......");
    }

    public void testInitMethod1() {
        // 执行顺序 4: 自定义的 init-method 方法
        log.info("TestBean testInitMethod1 exec......");
    }

    public void testInitMethod2() {
        log.info("TestBean testInitMethod2 exec......");
    }
}
